<?php

/**
 * BKTiny Framework
 * Core_Paginator class
 * 
 * LICENSE:
 * BKTiny Framework duoc xay dung va phat trien boi nhom BKloud - dai hoc bach khoa Ha Noi 
 * Ban duoc phep su dung framework nay cho muc dich hoc tap , nghien cuu va cac ung dung phi thuong mai
 * 
 * @copyright Copyright (c) 2011 BKloud - HPCC.
 * @version 0.1
 */
class Utilities_Paginator
{
    /**
     *adapter to use
     *
     * @var array
     */
    private $_adapter = array();

    /**
     * Chi so trang hien tai
     *
     * @var interger
     */
    private $_currentPageNumber = 1;

    /**
     * So luong item tren mot trang
     *
     * @var interger
     */
    private $_itemCountPerPage = 10;

    /**
     * Tong so item
     *
     * @var interger
     */
    private $_totalItemCount;

    /**
     * Chi so trang dau tien
     *
     * @var interger
     */
    private $_first = 1;

    /**
     * Chi so trang cuoi cung
     *
     * @var interger
     */
    private $_last = 1;

    /**
     * Chi so trang tiep theo trang hien tai  
     *
     * @var interger
     */
    private $_next = null;

    /**
     * Chi so trang truoc trang hien tai
     *
     * @var interger
     */
    private $_previous = null;

    /**
     * Base Url
     *
     * @var string
     */
    private $_baseUrl;

    public function __construct($adapter)
    {
        $this->_adapter = $adapter;
        $this->_totalItemCount = count($this->_adapter);

        $this->_last = (int)(($this->_totalItemCount - 1) / $this->_itemCountPerPage + 1);
        $this->_next = (($this->_currentPageNumber) < ($this->_last)) ? $this->
            _currentPageNumber + 1 : null;
        $this->_previous = (($this->_currentPageNumber) > ($this->_first)) ? $this->
            _currentPageNumber - 1 : null;

        $registry = Core_Registry::singleton();
        $this->_baseUrl = '/' . $registry['http']->getBaseUrl();
    }

    /**
     * thiet lap so luong item tren mot trang
     * 
     * @param $itemCountPerPage interger
     *
     * @return void
     */
    public function setItemCountPerPage($itemCountPerPage = null)
    {
        $num = (int)$itemCountPerPage;
        if ($num > 0)
            $this->_itemCountPerPage = $num;

        $this->_last = (int)(($this->_totalItemCount - 1) / $this->_itemCountPerPage + 1);
        $this->_next = (($this->_currentPageNumber) < ($this->_last)) ? $this->
            _currentPageNumber + 1 : null;
    }

    /**
     * thiet lap chi so trang hien tai
     * 
     * @param $currentPageNumber interger
     *
     * @return void
     */
    public function setCurrentPageNumber($currentPageNumber = null)
    {
        $num = (int)$currentPageNumber;
        if (($num > 0) && ($num <= $this->_last))
            $this->_currentPageNumber = $num;

        $this->_next = (($this->_currentPageNumber) < ($this->_last)) ? $this->
            _currentPageNumber + 1 : null;
        $this->_previous = (($this->_currentPageNumber) > ($this->_first)) ? $this->
            _currentPageNumber - 1 : null;

    }

    /**
     * Lay tong so item
     * 
     * @return interger
     */
    public function getTotalItemCount()
    {
        return $this->_totalItemCount;
    }

    /**
     * Lay chi so trang hien tai
     * 
     * @return interger
     */
    public function getCurrentPageNumber()
    {
        return $this->_currentPageNumber;
    }

    /**
     * Lay chi so trang dau tien
     * 
     * @return interger
     */
    public function getFirst()
    {
        return $this->_first;
    }

    /**
     * Lay chi so trang cuoi cung
     * 
     * @return interger
     */
    public function getLast()
    {
        return $this->_last;
    }

    /**
     * Lay chi so trang truoc trang hien tai
     * 
     * @return interger
     */
    public function getPrevious()
    {
        return $this->_previous;
    }

    /**
     * Lay chi so trang sau trang hien tai
     * 
     * @return interger
     */
    public function getNext()
    {
        return $this->_next;
    }


    /**
     * Lay so luong item tren 1 trang
     * 
     * @return interger
     */
    public function getItemCountPerPage()
    {
        return $this->_itemCountPerPage;
    }

    /**
     * Lay so luong item tren trang hien tai
     * 
     * @return interger
     */
    public function getCurrentItemCount()
    {
        if ($this->_currentPageNumber < $this->_last)
            return $this->_itemCountPerPage;
        $num = $this->_totalItemCount - $this->_itemCountPerPage * ($this->
            _currentPageNumber - 1);
        return $num;
    }

    /**
     * Lay ket qua phan trang
     * Mang cac item tren trang hien tai
     * @return array
     */
    public function getResult()
    {
        $result = array_slice($this->_adapter, $this->_itemCountPerPage * ($this->
            _currentPageNumber - 1), $this->getCurrentItemCount());
        return $result;
    }


    /**
     * Hien thi danh sach phan trang
     * @param string
     * @return void
     */
    public function paginatorControl($path)
    {
        $file = APPLICATION_PATH . $path;
        if (file_exists($file))
            include ($file);
        else
        {
            $registry = Core_Registry::singleton();
            $http     = $registry['http'];
            $module   = $http->getModule();

            $file = APPLICATION_PATH . '/modules/' . $module . '/views/scripts/' . $path;
            if (file_exists($file))
                include ($file);
            else
                echo "$path Not Found !";
        }
    }
}
